package com.huey.modules.oa.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huey.common.annotation.Log;
import com.huey.common.model.R;
import com.huey.db.base.model.PageResult;
import com.huey.modules.oa.dto.input.OaLeaveCreateInputDTO;
import com.huey.modules.oa.dto.input.OaLeaveSearchInputDTO;
import com.huey.modules.oa.dto.input.OaLeaveUpdateInputDTO;
import com.huey.modules.oa.dto.output.OaLeaveListOutputDTO;
import com.huey.modules.oa.dtomapper.MOaLeaveMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import com.huey.oa.model.OaLeave;
import com.huey.oa.service.OaLeaveService;
import org.springframework.web.bind.annotation.*;
import com.huey.db.base.controller.BaseController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huey
 * @since 2021-06-22
 */
@RestController
@RequestMapping("/oaLeave")
@Api(value = "OaLeaveController", tags = {"操作接口"})
public class OaLeaveController extends BaseController<OaLeaveService,OaLeave> {

    @Autowired
    private MOaLeaveMapper mOaLeaveMapper;

    @Log
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R<PageResult<OaLeaveListOutputDTO>> pageList(OaLeaveSearchInputDTO oaLeaveSearchInputDTO) {
        // 转换model
        OaLeave oaLeave = mOaLeaveMapper.toOaLeave(oaLeaveSearchInputDTO);
        // 获取源对象
        IPage<OaLeave> pageListByListInputDTO = baseService.getPageListByEntity(oaLeave);
        // 转换新对象
        List<OaLeaveListOutputDTO> oaLeaveListOutputDTOS = mOaLeaveMapper.toList(pageListByListInputDTO.getRecords());
        // 返回业务分页数据
        return success(toPageDTO(pageListByListInputDTO, oaLeaveListOutputDTOS));
    }

    @Log
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping("/save")
    public R<Boolean> save(@Validated @ApiParam @RequestBody OaLeaveCreateInputDTO oaLeaveCreateInputDTO) {
        OaLeave oaLeave = mOaLeaveMapper.toOaLeave(oaLeaveCreateInputDTO);
        return super.save(oaLeave);
    }

    @Log
    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping(value = "/update")
    public R<Boolean> update(@Validated @ApiParam @RequestBody OaLeaveUpdateInputDTO oaLeaveUpdateInputDTO) {
        OaLeave oaLeave = mOaLeaveMapper.toOaLeave(oaLeaveUpdateInputDTO);
        return super.update(oaLeave);
    }

    @Log
    @ApiOperation(value = "删除", notes = "删除")
    @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true)
    @PostMapping(value = "/delete")
    public R<Boolean> delete(@Validated @NotBlank(message = "id不能为空") @RequestParam String id) {
        return super.delete(id);
    }

    @Log
    @ApiOperation(value = "根据list删除", notes = "根据list删除")
    @PostMapping(value = "/deleteBatch")
    public R<Boolean> deleteBatch(@Validated @NotEmpty(message = "集合不能为空") @ApiParam @RequestBody List<String> idList) {
        return super.deleteBatch(idList);
    }

}
